package main;

import java.io.IOException;
import java.util.Random;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;

import base.RotatableSprite;

public class Res {
	
	public static int NUM_CHARACTER = 4;
	//public static int NUM_SWORD = 6;
	//public static int NUM_ARROW = 5;
	
	public static boolean soundOn = false;
	//public static int NUM_SUIT = 6;
	public static int ICON_SIZE = 30;
	public static int ITEM_ICON_OFFSET = 20;
	public static int SMALL_ICON_SIZE = 15;
	public static int SCR_W = 240;
	public static int SCR_H = 400;
	public static Image IMG_FOG;
	public static Image IMG_NEXT_LEVEL;
	public static Image IMG_BUTTON;
	public static Image IMG_ICON;
	public static Image IMG_MAP;
	public static Image IMG_SMALL_ICON;
	public static Image IMG_CHARACTER;
	public static Image IMG_AIR_MONSTER;
	public static Image IMG_ARM_1;
	public static Image IMG_ARM_2;
	public static Image IMG_BLOOD;
	public static Sprite SPT_ICON;
	public static Sprite SPT_SMALL_ICON;
	public static Random random;
	public static Image[] IMG_EFFECTS;
	public static Player[] sounds;

	public static void init() throws IOException, MediaException {
		random = new Random(System.currentTimeMillis());
		IMG_NEXT_LEVEL = Image.createImage("/next_level_panel.png"); 
		IMG_FOG = Image.createImage("/fog.png");
		IMG_BUTTON = Image.createImage("/large_button.png");
		IMG_ICON = Image.createImage("/icon.png");
		IMG_MAP = Image.createImage("/map.png");
		IMG_SMALL_ICON = Image.createImage("/small_icon.png");
		SPT_ICON = new Sprite(IMG_ICON, ICON_SIZE, ICON_SIZE);
		SPT_SMALL_ICON = new Sprite(IMG_SMALL_ICON, SMALL_ICON_SIZE,
				SMALL_ICON_SIZE);
		IMG_CHARACTER = Image.createImage("/character.png");
		IMG_AIR_MONSTER = Image.createImage("/air_monster.png");
		IMG_ARM_1 = Image.createImage("/arm1.png");
		IMG_ARM_2 = Image.createImage("/arm2.png");
		IMG_BLOOD = Image.createImage("/blood.png");

		IMG_EFFECTS = new Image[17];
		IMG_EFFECTS[0] = Image.createImage("/sword_slash.png");
		IMG_EFFECTS[1] = Image.createImage("/sword_slash.png");
		IMG_EFFECTS[2] = Image.createImage("/sword_slash_1.png");
		IMG_EFFECTS[3] = Image.createImage("/sword_slash_2.png");
		IMG_EFFECTS[4] = Image.createImage("/sword_slash_3.png");
		IMG_EFFECTS[5] = Image.createImage("/sword_slash_4.png");
		IMG_EFFECTS[6] = Image.createImage("/arrow.png");
		IMG_EFFECTS[7] = Image.createImage("/arrow.png");
		IMG_EFFECTS[8] = Image.createImage("/golden_arrow.png");
		IMG_EFFECTS[9] = Image.createImage("/fire_arrow.png");
		IMG_EFFECTS[10] = Image.createImage("/power_arrow.png");
		IMG_EFFECTS[11] = Image.createImage("/sword_slash.png");
		IMG_EFFECTS[12] = Image.createImage("/sword_slash.png");
		IMG_EFFECTS[13] = Image.createImage("/sword_slash_5.png");
		IMG_EFFECTS[14] = Image.createImage("/fire_ball.png");
		IMG_EFFECTS[15] = Image.createImage("/toxic_ball.png");
		IMG_EFFECTS[16] = Image.createImage("/bat_bullet.png");
		
		sounds = new Player[5];
		sounds[0] = Manager.createPlayer(Res.class.getResourceAsStream("/hit.wav"), "audio/X-wav");
		sounds[1] = Manager.createPlayer(Res.class.getResourceAsStream("/hero_die.wav"), "audio/X-wav");
		sounds[2] = Manager.createPlayer(Res.class.getResourceAsStream("/respawn.wav"), "audio/X-wav");
		sounds[3] = Manager.createPlayer(Res.class.getResourceAsStream("/lost_life.wav"), "audio/X-wav");
		sounds[4] = Manager.createPlayer(Res.class.getResourceAsStream("/click.wav"), "audio/X-wav");
		
		//heroDieSound.setLoopCount(1);
	}
	public static void PlaySound(int id){
		if(soundOn){
			try {
				sounds[id].stop();
				//heroDieSound.
				sounds[id].prefetch();
				//heroDieSound.realize();
				sounds[id].start();
			} catch (MediaException e) {
				e.printStackTrace();
			}
		}
	}
	public static int getRandom(int n) {
		return random.nextInt(n);
	}

	public static void drawIcon(Graphics g, int i, int x, int y) {
		if (i < 0) {
			return;
		}
		SPT_ICON.setPosition(x, y);
		SPT_ICON.setFrame(i);
		SPT_ICON.paint(g);
	}

	public static void drawSmallIcon(Graphics g, int i, int x, int y) {
		SPT_SMALL_ICON.setPosition(x, y);
		SPT_SMALL_ICON.setFrame(i + NUM_CHARACTER);
		SPT_SMALL_ICON.paint(g);
	}

	public static Sprite createHandSprite(int i) {
		Sprite sprite = new Sprite(IMG_SMALL_ICON, 15, 15);
		sprite.defineReferencePixel(7, 8);
		sprite.setFrame(i);
		return sprite;
	}

	public static RotatableSprite createCharacterSprite(int i) {
		RotatableSprite sprite;
		if(i<4){
			sprite = new RotatableSprite(IMG_CHARACTER, 30, 30, 1);
			sprite.defineReferencePixel(15, 15);
			sprite.setFrameSequence(new int[] { i * 3 });
			return sprite;
		}
		sprite = new RotatableSprite(IMG_AIR_MONSTER, 60, 35, 0);
		sprite.defineReferencePixel(30, 18);
		sprite.setFrameSequence(new int[] { i - 4});
		return sprite;
	}

	public static RotatableSprite createArm1Sprite(int i) {
		RotatableSprite sprite;
		sprite = new RotatableSprite(IMG_ARM_1, 30, 30, 1);
		sprite.setFrameSequence(new int[] { i * 3 });
		return sprite;
	}
	public static RotatableSprite createArm2Sprite(int i) {
		RotatableSprite sprite;
		int index = i * 6;
		sprite = new RotatableSprite(IMG_ARM_2, 30, 30, 2);
		sprite.setFrameSequence(new int[] { index, index + 1 });
		return sprite;
	}

	public static Sprite createFenceSprite() {
		int i = NUM_CHARACTER * 3;
		Sprite sprite = new Sprite(IMG_CHARACTER, 30, 30);
		sprite.defineReferencePixel(15, 15);
		sprite.setFrameSequence(new int[] { i, i + 1, i + 2 });
		return sprite;
	}

	public static RotatableSprite createBloodSprite() {
		RotatableSprite sprite = new RotatableSprite(IMG_BLOOD, 15, 15, 0);
		sprite.setFrameSequence(new int[] { 0, 1, 2, 3, 4 });
		return sprite;
	}
}
